In [1]:
%matplotlib inline

def iffie_innie(x1,y1,x2,y2,max_x, max_y)

def iffie_innie(x1,y1,x2,y2,max_x, max_y):
    """ determine if the box is within the image
    """
    if x1 > 0 and y1 > 0 and x2 < max_x and y2 < max_y:
        return True
    else:
        return False
In [2]:
import time
nb_start_time = time.time()

import os
import tempfile
import sys

from collections import OrderedDict
import argparse

import tensorflow as tf
from tensorflow import io as tf_io

import numpy as np
import pandas as pd
import yaml

from skimage.filters import threshold_otsu
from skimage.color import rgb2lab

from PIL import ImageDraw
from PIL import TiffImagePlugin as tip

import IPython.display as ip_display

import openslide

sys.path.insert(0, '../src/python')
from digipath_toolkit import *

def iffie_innie(x1,y1,x2,y2,max_x, max_y):
    """ Usage: True_False = iffie_innie(x1, y1, x2, y2, max_x, max_y)
                determine if the box is within the image
    """
    if x1 > 0 and y1 > 0 and x2 < max_x and y2 < max_y:
        return True
    else:
        return False


def im_pair_hori(im_0, im_1):
    """ Usage: new_im = im_pair_hori(im_0, im_1)
            combine a list of PIL images horizontaly
    """
    w0 = im_0.size[0]
    w = w0 + im_1.size[0] + 1
    h = max(im_0.size[1], im_1.size[1])

    new_im = tip.Image.new('RGB', (w, h) )
    box = (0, 0, w0, h)
    new_im.paste(im_0, box)
    
    box = (w0+1, 0, w, h)
    new_im.paste(im_1, box)

    return new_im
In [3]:
test_data_dir = '../../DigiPath_MLTK_data/RegistrationDevData/'
os.listdir(test_data_dir)

offset_data_file = os.path.join(test_data_dir, 'wsi_pair_sample.csv')
if os.path.isfile(offset_data_file):
    offset_df = pd.read_csv(offset_data_file)
    
offset_x = offset_df['truth_offset_x'].iloc[0]
offset_y = offset_df['truth_offset_y'].iloc[0]
offset_x, offset_y = int(round(offset_x)), int(round(offset_y))

auto_x = offset_df['auto_offset_x'].iloc[0]
auto_y = offset_df['auto_offset_y'].iloc[0]
auto_x, auto_y = int(round(auto_x)), int(round(auto_y))

print('\noffset_x, offset_y, auto_x, auto_y\n', offset_x, offset_y, auto_x, auto_y)

fixed_wsi = os.path.join(test_data_dir, '54742d6c5d704efa8f0814456453573a.tiff')

fixed_levels_dict = get_level_sizes_dict(fixed_wsi)
fixed_max_width = fixed_levels_dict['image_size'][0]
fixed_max_height = fixed_levels_dict['image_size'][1]

print('\n\nfixed_wsi image: 54742d6c5d704efa8f0814456453573a.tiff')
for k, v in fixed_levels_dict.items():
    print('%25s: %s'%(k,v))
    
float_wsi = os.path.join(test_data_dir, 'e39a8d60a56844d695e9579bce8f0335.tiff')

float_levels_dict = get_level_sizes_dict(float_wsi)
float_max_width = float_levels_dict['image_size'][0]
float_max_height = float_levels_dict['image_size'][1]

print('\n\nfloat_wsi image: e39a8d60a56844d695e9579bce8f0335.tiff')
for k, v in float_levels_dict.items():
    print('%25s: %s'%(k,v))
offset_x, offset_y, auto_x, auto_y
 -1618 1673 -1621 1676


fixed_wsi image: 54742d6c5d704efa8f0814456453573a.tiff
               image_size: (145408, 83968)
              level_count: 10
        level_downsamples: (1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0)
         level_diminsions: ((145408, 83968), (72704, 41984), (36352, 20992), (18176, 10496), (9088, 5248), (4544, 2624), (2272, 1312), (1136, 656), (568, 328), (284, 164))


float_wsi image: e39a8d60a56844d695e9579bce8f0335.tiff
               image_size: (126976, 75776)
              level_count: 9
        level_downsamples: (1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0)
         level_diminsions: ((126976, 75776), (63488, 37888), (31744, 18944), (15872, 9472), (7936, 4736), (3968, 2368), (1984, 1184), (992, 592), (496, 296))
In [4]:
try:
    wsi_obj_0.close()
except:
    print('wsi_obj_0 object De Nada')
wsi_obj_0 object De Nada
In [14]:
run_parameters = {'wsi_filename': fixed_wsi, 
                  'wsi_floatname': float_wsi,
                  'thumbnail_divisor': 20, 
                  'patch_select_method': 'threshold_rgb2lab', 
                  'rgb2lab_threshold': 80, 
                  'image_level': 0, 
                  'patch_height': 224, 
                  'patch_width': 224, 
                  'threshold': 0, 
                  'offset_x': offset_x, 
                  'offset_y': offset_y}

patch_igetor = PatchImageGenerator(run_parameters)
wsi_obj_0 = openslide.OpenSlide(run_parameters['wsi_floatname'])
In [15]:
n_2_try = 100

print('offset_x ', run_parameters['offset_x'], '\t', 'offset_y ', run_parameters['offset_y'], '\n')

for _ in range(n_2_try):
    nptch_dict = patch_igetor.next_patch()
    fxd_im = nptch_dict['patch_image']

    image_level = run_parameters['image_level']
    patch_size = (run_parameters['patch_width'], run_parameters['patch_height'])

    x1 = nptch_dict['level_0_x'] - run_parameters['offset_x']
    x2 = x1 + run_parameters['patch_width']
    y1 = nptch_dict['level_0_y'] - run_parameters['offset_y']
    y2 = y1 + run_parameters['patch_height']

    print('fixed_x: %i float_x: %i\tfixed_y: %i float_y: %i'%(nptch_dict['level_0_x'], x1,
                                                              nptch_dict['level_0_y'], y1))

    if iffie_innie(x1, y1, x2, y2, float_max_width, float_max_height) == True:
        flot_im = wsi_obj_0.read_region((x1, y1), image_level, patch_size)
        display(im_pair_hori(fxd_im, flot_im))
    else:
        print('out of bounds')
offset_x  -1618 	 offset_y  1673 

fixed_x: 27344 float_x: 28962	fixed_y: 96 float_y: -1577
out of bounds
fixed_x: 27568 float_x: 29186	fixed_y: 96 float_y: -1577
out of bounds
fixed_x: 45040 float_x: 46658	fixed_y: 96 float_y: -1577
out of bounds
fixed_x: 77520 float_x: 79138	fixed_y: 96 float_y: -1577
out of bounds
fixed_x: 115824 float_x: 117442	fixed_y: 96 float_y: -1577
out of bounds
fixed_x: 118736 float_x: 120354	fixed_y: 96 float_y: -1577
out of bounds
fixed_x: 15472 float_x: 17090	fixed_y: 320 float_y: -1353
out of bounds
fixed_x: 15696 float_x: 17314	fixed_y: 320 float_y: -1353
out of bounds
fixed_x: 27344 float_x: 28962	fixed_y: 320 float_y: -1353
out of bounds
fixed_x: 27568 float_x: 29186	fixed_y: 320 float_y: -1353
out of bounds
fixed_x: 41904 float_x: 43522	fixed_y: 320 float_y: -1353
out of bounds
fixed_x: 25104 float_x: 26722	fixed_y: 544 float_y: -1129
out of bounds
fixed_x: 27568 float_x: 29186	fixed_y: 544 float_y: -1129
out of bounds
fixed_x: 28016 float_x: 29634	fixed_y: 544 float_y: -1129
out of bounds
fixed_x: 41680 float_x: 43298	fixed_y: 544 float_y: -1129
out of bounds
fixed_x: 63632 float_x: 65250	fixed_y: 544 float_y: -1129
out of bounds
fixed_x: 106864 float_x: 108482	fixed_y: 544 float_y: -1129
out of bounds
fixed_x: 110000 float_x: 111618	fixed_y: 544 float_y: -1129
out of bounds
fixed_x: 9424 float_x: 11042	fixed_y: 768 float_y: -905
out of bounds
fixed_x: 15472 float_x: 17090	fixed_y: 992 float_y: -681
out of bounds
fixed_x: 22864 float_x: 24482	fixed_y: 992 float_y: -681
out of bounds
fixed_x: 48176 float_x: 49794	fixed_y: 992 float_y: -681
out of bounds
fixed_x: 26448 float_x: 28066	fixed_y: 1216 float_y: -457
out of bounds
fixed_x: 102608 float_x: 104226	fixed_y: 1216 float_y: -457
out of bounds
fixed_x: 74384 float_x: 76002	fixed_y: 1440 float_y: -233
out of bounds
fixed_x: 102608 float_x: 104226	fixed_y: 1440 float_y: -233
out of bounds
fixed_x: 71920 float_x: 73538	fixed_y: 1664 float_y: -9
out of bounds
fixed_x: 1136 float_x: 2754	fixed_y: 1888 float_y: 215
fixed_x: 1360 float_x: 2978	fixed_y: 1888 float_y: 215
fixed_x: 1808 float_x: 3426	fixed_y: 1888 float_y: 215
fixed_x: 17264 float_x: 18882	fixed_y: 1888 float_y: 215
fixed_x: 1136 float_x: 2754	fixed_y: 2112 float_y: 439
fixed_x: 16816 float_x: 18434	fixed_y: 2112 float_y: 439
fixed_x: 43920 float_x: 45538	fixed_y: 2112 float_y: 439
fixed_x: 59152 float_x: 60770	fixed_y: 2112 float_y: 439
fixed_x: 59376 float_x: 60994	fixed_y: 2112 float_y: 439
fixed_x: 94096 float_x: 95714	fixed_y: 2112 float_y: 439
fixed_x: 103280 float_x: 104898	fixed_y: 2112 float_y: 439
fixed_x: 50416 float_x: 52034	fixed_y: 2336 float_y: 663
fixed_x: 54896 float_x: 56514	fixed_y: 2336 float_y: 663
fixed_x: 27120 float_x: 28738	fixed_y: 2560 float_y: 887
fixed_x: 87376 float_x: 88994	fixed_y: 2560 float_y: 887
fixed_x: 119184 float_x: 120802	fixed_y: 2560 float_y: 887
fixed_x: 16592 float_x: 18210	fixed_y: 2784 float_y: 1111
fixed_x: 16816 float_x: 18434	fixed_y: 2784 float_y: 1111
fixed_x: 21520 float_x: 23138	fixed_y: 2784 float_y: 1111
fixed_x: 21744 float_x: 23362	fixed_y: 2784 float_y: 1111
fixed_x: 73040 float_x: 74658	fixed_y: 2784 float_y: 1111
fixed_x: 34064 float_x: 35682	fixed_y: 3008 float_y: 1335
fixed_x: 34288 float_x: 35906	fixed_y: 3008 float_y: 1335
fixed_x: 40560 float_x: 42178	fixed_y: 3008 float_y: 1335
fixed_x: 51760 float_x: 53378	fixed_y: 3008 float_y: 1335
fixed_x: 54224 float_x: 55842	fixed_y: 3008 float_y: 1335
fixed_x: 54448 float_x: 56066	fixed_y: 3008 float_y: 1335
fixed_x: 90960 float_x: 92578	fixed_y: 3008 float_y: 1335
fixed_x: 8304 float_x: 9922	fixed_y: 3232 float_y: 1559
fixed_x: 8528 float_x: 10146	fixed_y: 3232 float_y: 1559
fixed_x: 13456 float_x: 15074	fixed_y: 3232 float_y: 1559
fixed_x: 58704 float_x: 60322	fixed_y: 3232 float_y: 1559
fixed_x: 90960 float_x: 92578	fixed_y: 3232 float_y: 1559
fixed_x: 11664 float_x: 13282	fixed_y: 3456 float_y: 1783
fixed_x: 15248 float_x: 16866	fixed_y: 3456 float_y: 1783
fixed_x: 27344 float_x: 28962	fixed_y: 3456 float_y: 1783
fixed_x: 31152 float_x: 32770	fixed_y: 3456 float_y: 1783
fixed_x: 51536 float_x: 53154	fixed_y: 3456 float_y: 1783
fixed_x: 65648 float_x: 67266	fixed_y: 3456 float_y: 1783
fixed_x: 65648 float_x: 67266	fixed_y: 3680 float_y: 2007
fixed_x: 66320 float_x: 67938	fixed_y: 3680 float_y: 2007
fixed_x: 5840 float_x: 7458	fixed_y: 3904 float_y: 2231
fixed_x: 6064 float_x: 7682	fixed_y: 3904 float_y: 2231
fixed_x: 6288 float_x: 7906	fixed_y: 3904 float_y: 2231
fixed_x: 6512 float_x: 8130	fixed_y: 3904 float_y: 2231
fixed_x: 27344 float_x: 28962	fixed_y: 3904 float_y: 2231
fixed_x: 33616 float_x: 35234	fixed_y: 3904 float_y: 2231
fixed_x: 59376 float_x: 60994	fixed_y: 3904 float_y: 2231
fixed_x: 78416 float_x: 80034	fixed_y: 3904 float_y: 2231
fixed_x: 5616 float_x: 7234	fixed_y: 4128 float_y: 2455
fixed_x: 5840 float_x: 7458	fixed_y: 4128 float_y: 2455
fixed_x: 6064 float_x: 7682	fixed_y: 4128 float_y: 2455
fixed_x: 6288 float_x: 7906	fixed_y: 4128 float_y: 2455
fixed_x: 6512 float_x: 8130	fixed_y: 4128 float_y: 2455
fixed_x: 16816 float_x: 18434	fixed_y: 4128 float_y: 2455
fixed_x: 27792 float_x: 29410	fixed_y: 4128 float_y: 2455
fixed_x: 78416 float_x: 80034	fixed_y: 4128 float_y: 2455
fixed_x: 78640 float_x: 80258	fixed_y: 4128 float_y: 2455
fixed_x: 90064 float_x: 91682	fixed_y: 4128 float_y: 2455
fixed_x: 5392 float_x: 7010	fixed_y: 4352 float_y: 2679
fixed_x: 5616 float_x: 7234	fixed_y: 4352 float_y: 2679
fixed_x: 5840 float_x: 7458	fixed_y: 4352 float_y: 2679
fixed_x: 6064 float_x: 7682	fixed_y: 4352 float_y: 2679
fixed_x: 8528 float_x: 10146	fixed_y: 4352 float_y: 2679
fixed_x: 10096 float_x: 11714	fixed_y: 4352 float_y: 2679
fixed_x: 10320 float_x: 11938	fixed_y: 4352 float_y: 2679
fixed_x: 14800 float_x: 16418	fixed_y: 4352 float_y: 2679
fixed_x: 26672 float_x: 28290	fixed_y: 4352 float_y: 2679
fixed_x: 31152 float_x: 32770	fixed_y: 4352 float_y: 2679
fixed_x: 31376 float_x: 32994	fixed_y: 4352 float_y: 2679
fixed_x: 60496 float_x: 62114	fixed_y: 4352 float_y: 2679
fixed_x: 78416 float_x: 80034	fixed_y: 4352 float_y: 2679
fixed_x: 123664 float_x: 125282	fixed_y: 4352 float_y: 2679
In [16]:
try:
    wsi_obj_0.close()
except:
    print('wsi_obj_0 object De Nada')
In [ ]: